<template>
  <view class="setting-container setting-popup">
    <button @click="emitClose" class="close-btn">关闭</button>
    <view class="setting-title">
      <text>设置</text>
    </view>
    <view class="setting-list">
      <view class="setting-item">
        <text class="item-label">背景音乐</text>
        <switch :checked="musicOff" @change="handleMusicChange" />
      </view>
      <view class="setting-item">
        <text class="item-label">声音</text>
        <switch :checked="soundOff" @change="handleSoundChange" />
      </view>
      <view class="setting-item">
        <text class="item-label">震动</text>
        <switch :checked="vibrateOff" @change="handleVibrateChange" />
      </view>
      <view class="setting-item feedback-item">
        <text class="item-label">意见反馈</text>
        <button @click="feedback" class="feedback-btn">去反馈</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { storageUtil } from '@/utils/storage.js'  // 引入存储工具
import { STORAGE_KEYS } from '@/utils/constants.js'  // 引入常量工具

const emit = defineEmits(['close', 'update:musicOff', 'update:soundOff', 'update:vibrateOff'])

// 状态管理
const musicOff = ref(false)
const soundOff = ref(false)
const vibrateOff = ref(false)

// 从本地存储加载设置
onMounted(() => {
  // 使用存储工具和常量工具获取值，并指定默认值
  musicOff.value = storageUtil.get(STORAGE_KEYS.musicOff, false)
  soundOff.value = storageUtil.get(STORAGE_KEYS.soundOff, false)
  vibrateOff.value = storageUtil.get(STORAGE_KEYS.vibrateOff, false)
  
  // 初始化时同步到父组件
  emit('update:musicOff', musicOff.value)
  emit('update:soundOff', soundOff.value)
  emit('update:vibrateOff', vibrateOff.value)
})

// 监听设置变化并保存
watch(musicOff, (newVal) => {
  storageUtil.set(STORAGE_KEYS.musicOff, newVal)
  emit('update:musicOff', newVal)
})

watch(soundOff, (newVal) => {
  storageUtil.set(STORAGE_KEYS.soundOff, newVal)
  emit('update:soundOff', newVal)
})

watch(vibrateOff, (newVal) => {
  storageUtil.set(STORAGE_KEYS.vibrateOff, newVal)
  emit('update:vibrateOff', newVal)
})

// 事件处理函数
const emitClose = () => emit('close')

const handleMusicChange = (e) => {
  musicOff.value = e.detail.value
}

const handleSoundChange = (e) => {
  soundOff.value = e.detail.value
}

const handleVibrateChange = (e) => {
  vibrateOff.value = e.detail.value
}

const feedback = () => {
  uni.showToast({ title: '意见反馈开发中', icon: 'none' })
}
</script>

<style scoped>
/* 样式保持不变 */
.setting-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60rpx 40rpx;
  box-sizing: border-box;
}

.setting-title {
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 50rpx;
  color: #333;
}

.setting-list {
  width: 100%;
  max-width: 600rpx;
}

.setting-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding: 25rpx 0;
  font-size: 32rpx;
  border-bottom: 1px solid #f5f5f5;
}

.setting-item:last-child {
  border-bottom: none;
}

.item-label {
  color: #333;
  white-space: nowrap;
}

.close-btn {
  position: absolute;
  right: 30rpx;
  top: 30rpx;
  z-index: 10;
  padding: 15rpx 25rpx;
  font-size: 28rpx;
  background-color: #f5f5f5;
  border-radius: 8rpx;
}

.feedback-item {
  padding-top: 30rpx;
}

.feedback-btn {
  padding: 15rpx 30rpx;
  font-size: 28rpx;
  background-color: #007aff;
  color: white;
  border-radius: 8rpx;
}

.setting-popup {
  background: #fff;
  border-radius: 20rpx;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.18);
  min-width: 600rpx;
  min-height: 400rpx;
  position: relative;
  z-index: 1002;
  opacity: 1;
  margin: 0 auto;
}
</style>
